package conqueror.com.camerarecord;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Created by Administrator on 2016/5/25 0025.
 */
public class AboutCreateWindow {
    private WindowManager wm;
    private WindowManager.LayoutParams params;

    public AboutCreateWindow(Context context) {
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams();
    }

    /**
     * 创建窗口
     *
     * @param fl(容器)
     */
    public void createFloatWindow(FrameLayout fl) {
        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        params.format = PixelFormat.TRANSPARENT; // 设置图片格式，效果为背景透明
        params.gravity = Gravity.LEFT | Gravity.TOP;
        //这里随便设置的
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.x = 0;
        params.y = 0;
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        params.width = dm.widthPixels * 2 / 3;
        params.height = dm.heightPixels * 2 / 3;
//        params.width = DAndPUtil.Dp2Px(this, 300);
//        params.height = DAndPUtil.Dp2Px(this, 300);
        wm.addView(fl, params);
    }

    /**
     * 开启窗口，等于刷新一个预览窗口
     *
     * @param fl(容器)
     */
    public void openWindow(FrameLayout fl) {
        params.x = 0;
        params.y = 0;
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        params.width = dm.widthPixels * 2 / 3;
        params.height = dm.heightPixels * 2 / 3;
//        params.width = DAndPUtil.Dp2Px(this, 600);
//        params.height = DAndPUtil.Dp2Px(this, 300);
        wm.updateViewLayout(fl, params);
    }

    /**
     * 隐藏窗口
     *
     * @param fl
     */
    public void hideFloatWindow(FrameLayout fl) {
        params.width = 1;
        params.height = 1;
        wm.updateViewLayout(fl, params);
    }

    public void removeWindow(FrameLayout fl) {
        wm.removeView(fl);
        if (wm != null) {
            wm = null;
        }
    }
}
